package com.data.basic.chapter05;

/** 删除链表的重复元素的递归实现2=====》打印信息版本
 * Created by wangbing on 2018/5/5.
 */
public class Solution5 {

    /**
     * 只考虑宏观语义： 整个方法的作用是删除val元素的节点，返回新的list
     *      那么开始遍历list，那么list的第一个节点head是要删除的val，那么剩下的(head.next)就是返回值res
     *      如果（head）不是的话，那就变成返回值res就是head;
     *
     * @param head
     * @param val
     * @return
     */
    public ListNode removeElements(ListNode head, int val,int depth) {
        String depthString = generateDepthString(depth);
        System.out.println(depthString+"call :remove"+val+" in "+head);
       if (head==null){
           System.out.println(depthString+"Return:"+head);

           return null;
       }
        ListNode res= removeElements(head.next,val,depth+1);
        System.out.println(depthString+"After remove"+val+":"+res);
        ListNode ret;

        if (head.val==val){
            ret = res;
        }else {
            head.next = res;
            ret = head;
        }
        System.out.println(depthString+"Return:"+ret);
        return ret;
    }

    private String generateDepthString(int depth) {
        StringBuilder res = new StringBuilder();
        for (int i=0;i<depth;i++){
            res.append("--");
        }
        return res.toString();

    }


    public static void main(String[] args) {
        int nums[] = {1,2,6,3,4,5,6};
        ListNode head = new ListNode(nums);
        System.out.println(head);

        ListNode res = (new Solution5()).removeElements(head,6,0);
        System.out.println(res);
    }
}
